package cn.ctvonline.android.kaopu.base;

import android.app.Application;
import android.content.Context;
import android.graphics.Bitmap;

import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;
import com.tencent.bugly.crashreport.CrashReport;

import java.io.File;

import cn.ctvonline.android.kaopu.BuildConfig;
import cn.ctvonline.android.kaopu.R;
import cn.ctvonline.android.kaopu.constant.Constant;
import cn.ctvonline.android.kaopu.support.AmapLocationManager;
import cn.ctvonline.android.kaopu.support.net.HttpComponent;

/**
 * Created by dujian on 15/11/13.
 */
public class AppApplication extends Application {
    private DisplayImageOptions options;
    private static AppApplication instance;
    private static boolean isDebug = false;

    public void onCreate() {
        super.onCreate();
        initImageLoader(getApplicationContext());
        initGaoDeLocation(getApplicationContext());
        instance = this;
        isDebug = BuildConfig.DEBUG;
        initBugly();
    }

    private void initHttpComp() {
        HttpComponent.Builder builder = new HttpComponent.Builder();
        HttpComponent.getInstance().init(builder.build());
    }

    private void initImageLoader(Context context) {
        File cacheDir = StorageUtils.getCacheDirectory(context);
        ImageLoaderConfiguration.Builder builder = new ImageLoaderConfiguration.Builder(context);
        builder.threadPriority(Thread.NORM_PRIORITY - 2);
        builder.diskCacheFileNameGenerator(new Md5FileNameGenerator());
        builder.denyCacheImageMultipleSizesInMemory();//拒绝缓存相同图片
        builder.diskCacheSize(50 * 1024 * 1024);//30 M
        builder.tasksProcessingOrder(QueueProcessingType.LIFO);
        builder.memoryCache(new LruMemoryCache(2 * 1024 * 1024)); //内存缓存2M
        builder.diskCacheFileCount(200);//200 个缓存文件
        builder.diskCache(new UnlimitedDiskCache(cacheDir));
        builder.imageDownloader(new BaseImageDownloader(context, 5 * 1000, 20 * 1000)); //链接超时和下载超时时间
        //具体缓存的设置
        DisplayImageOptions.Builder optBuilder = new DisplayImageOptions.Builder();
        optBuilder.imageScaleType(ImageScaleType.EXACTLY_STRETCHED);
        optBuilder.showImageOnLoading(R.mipmap.logo);
        optBuilder.displayer(new FadeInBitmapDisplayer(200));
        optBuilder.cacheInMemory(true);//设置内存缓存
        optBuilder.cacheOnDisk(true);//设置磁盘缓存
        optBuilder.bitmapConfig(Bitmap.Config.RGB_565);
        options = optBuilder.build();
        builder.defaultDisplayImageOptions(options);
        ImageLoader.getInstance().init(builder.build());
    }

    private void initBugly() {
        CrashReport.initCrashReport(getApplicationContext(), Constant.BUGLY_APP_ID, false);
    }

    public void initGaoDeLocation(Context context) {
        AmapLocationManager locManager = AmapLocationManager.getInstance();
        locManager.init(context);
        locManager.startLocation(null);
    }

    @Override
    public void onTerminate() {
        ImageLoader.getInstance().clearMemoryCache();
        ImageLoader.getInstance().destroy();
        AmapLocationManager.getInstance().destoryLocation();
        super.onTerminate();
    }

    public static AppApplication getInstance() {
        return instance;
    }

    public DisplayImageOptions getOptions() {
        return options;
    }
}
